package org.guohao.mockdata.controller;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.input.PickResult;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.guohao.mockdata.action.NewConnectAction;
import org.guohao.mockdata.constant.DBType;
import org.guohao.mockdata.entity.DBConnectEntity;
import org.guohao.mockdata.utils.GlobalContextMenu;
import org.guohao.mockdata.utils.SyncManager;

public class HomeController implements Initializable {

  @FXML
  public MenuButton connectBtn;
  public Stage mainStage;
  public TreeItem<String> connectList;
  public TreeView<String> treeView;
  private SyncManager syncManager;

  @Override
  public void initialize(URL location, ResourceBundle resources) {
    syncManager = new SyncManager(this);
    List<DBConnectEntity> dbList = syncManager.getDBList();
    for (DBConnectEntity dbConnectEntity :
        dbList) {
      TreeItem<String> stringTreeItem = new TreeItem<>(dbConnectEntity.getName());
      connectList.getChildren().add(stringTreeItem);
    }
  }

  public void addConnectListItem(DBConnectEntity dbConnectInfo) {
    connectList.getChildren().add(new TreeItem<>(dbConnectInfo.getName()));
  }

  public void handlerClick(ActionEvent actionEvent) {
    MenuItem menuItem = (MenuItem) actionEvent.getTarget();
    String dbTypeStr = menuItem.getText();
    DBType dbType = DBType.valueOf(dbTypeStr);
    Stage connectSetting = new Stage();
    FXMLLoader fxmlLoader = new FXMLLoader(
        HomeController.class.getResource("/fxml/connect.fxml"));
    Scene scene = null;
    try {
      scene = new Scene(fxmlLoader.load(), mainStage.getWidth() * 0.7,
          mainStage.getHeight() * 0.7);
    } catch (IOException e) {
      e.printStackTrace();
    }
    connectSetting.initOwner(mainStage);
    ConnectController controller = fxmlLoader.getController();
    controller.setSyncManager(syncManager);
    controller.dbType = dbType;
    controller.port.setText(dbType.getDefaultPort().toString());
    connectSetting.initModality(Modality.APPLICATION_MODAL);
    connectSetting.setTitle("新建连接-" + dbTypeStr);
    connectSetting.setScene(scene);
    connectSetting.show();
  }

  private List<MenuItem> treeViewContext(Object node) {
    NewConnectAction newConnectAction = new NewConnectAction(mainStage, syncManager);
    List<MenuItem> menuItems = new ArrayList<>();
    Menu newConnect = new Menu("新建连接");
    MenuItem dm = new MenuItem("DM");
    MenuItem mysql = new MenuItem("MYSQL");
    MenuItem oracle = new MenuItem("ORACLE");
    dm.setOnAction(newConnectAction);
    mysql.setOnAction(newConnectAction);
    oracle.setOnAction(newConnectAction);
    newConnect.getItems().addAll(dm, mysql, oracle);
    menuItems.add(newConnect);
    String nodeStr = node.toString();
    if (nodeStr.startsWith("Text")) {
      MenuItem connect = new MenuItem("连接");
      MenuItem edit = new MenuItem("编辑");
      MenuItem delete = new MenuItem("删除");
      menuItems.add(connect);
      menuItems.add(edit);
      menuItems.add(delete);
    }
    return menuItems;
  }

  public void showContext(ContextMenuEvent contextMenuEvent) {
    PickResult pickResult = contextMenuEvent.getPickResult();
    Node node = pickResult.getIntersectedNode();
    GlobalContextMenu.getInstance().show(contextMenuEvent,treeViewContext(node));
  }
}